/**
 * @namespace
 */
aws.modules.ui = {
	listSubscribers: function() {
		return {
			"init": this.init,
			"keypress": this.keypress,
			"rootWaypointSet": this.show,
			"goneToNextWaypoint": this.goneToNextWaypoint,
			"goneToPreviousWaypoint": this.goneToPreviousWaypoint,
			"waypointValueChanged": this.waypointValueChanged,
			"rootWaypointUnset": [this.rootWaypointUnset, this.hide, this.reset],
			"reset": [this.hide, this.reset]
		}
	},
	/**
	 * Prepends UI's panel and subscribes to various events
	 */
	init: function() {
		$('<div id="aws"><ul class="path" /><div class="extras"><div class="shortcut"><span class="keys" /><span class="value" /></div></div></div>').prependTo('body');
		$('#aws ul.path li.waypoint h1').live('click', function() {
			$(this).siblings('.assistant').toggle().parent().toggleClass('active');
			return false;
		});
	},
	/**
	 * Simply shows UI panel
	 */
	show: function() {
		$('#aws').show();
	},
	/**
	 * Hides UI panel
	 */
	hide: function() {
		$('#aws').hide();
	},
	/**
	 * Removes info about waypoints
	 */
	reset: function() {
		$('#aws .extras .shortcut .keys, #aws .extras .shortcut .value').empty();
		$('#aws ul.path').empty();
	},
	/**
	 * Prevents browser to go to previous page when it should not
	 * @param {String} code Code of key pressed
	 * @param {Object} e Event
	 */
	keypress: function(code, e, type) {
		if (code == 8 && this.waypoint) {
			e.preventDefault();
		}
	},
	/**
	 * Appends info about new waypoint to UI panel
	 */
	goneToNextWaypoint: function() {
		var waypointClass = 'waypoint', html, that = this;
		
		if (typeof this.waypoint.value != 'undefined') {
			 waypointClass += ' value';
		}
		if (this.waypoint.onEnter) {
			 waypointClass += ' executable';
		}
		if (this.waypoint.next && typeof this.waypoint.next.value != 'undefined') {
			 waypointClass += ' next-is-value';
		} else if (this.waypoint.next) {
			 waypointClass += ' has-children';
		}
		
		html = '<li class="' + waypointClass + '">';
		html += '<h1>' + this.waypoint.name + '</h1>';
		html += '<div class="assistant">';
		html += '<p class="description">' + this.waypoint.description + '</p>';
		if (this.waypoint.onEnter && this.waypoint.onEnter.description) {
			html += '<p class="onenter"><span class="key">Enter</span> ' + this.waypoint.onEnter.description + '</p>';
		}
		if (this.waypoint.next && typeof this.waypoint.next.value != 'undefined') {
			html += '<p class="value"><span class="key">Type</span> ' + this.waypoint.next.description + '</p>';
		} else if (this.waypoint.next ) {
			var allnext = '';
			html += '<ul class="next">';
			$.each(this.waypoint.next, function(k, v) {
				allnext += '<li><span class="key">' + k + '</span> ' + v.name + '</li>';
			});
			html += allnext;
			html += '</ul>';
		}
		html += '</div></li>';
		$('#aws ul.path').append(html);
		
		if (this.waypoint.key) {
			$('#aws .extras .shortcut .keys').text(function() {
				return $(this).text() + that.waypoint.key;
			});
		}
	},
	/**
	 * Removes info about last waypoint from UI panel
	 */
	goneToPreviousWaypoint: function() {
		$('#aws ul.path li.waypoint:last').remove();
		if ($('#aws .extras .shortcut .value').text().length !== 0) {
			$('#aws .extras .shortcut .value').empty();
		} else {
			$('#aws .extras .shortcut .keys').text(function() {
				return $(this).text().slice(0, -1);
			});
		}
	},
	/**
	 * Displays new value of waypoint in UI panel
	 */
	waypointValueChanged: function() {
		$('#aws ul.path li.waypoint:last h1, #aws .extras .shortcut .value').text(this.waypoint.value);
	},
	rootWaypointUnset: function() {
		$('#aws .extras .shortcut span').empty();
	}
};
